<?php

namespace App\Models;

use App\Models\BaseModel as Model;
// use Illuminate\Database\Eloquent\Model;

class OrderLog extends Model
{

    /* 操作行为 */
    const ORDER_CHANGE               = 0; // 订单创建
    const ORDER_PAID                 = 1; // 订单支付
    const ORDER_DELIVERY             = 2; // 订单发货
    const ORDER_INVALID              = 3; // 订单作废
    const ORDER_COMPLATE             = 4; // 订单完成

    // 白名单
    protected $fillable = ['order_id', 'order_bn', 'source', 'op_id', 'op_name', 'member_id', 'behavior', 'status', 'content'];

    protected $appends = ['member_name'];

    // 黑名单
    protected $casts = [
        'created_at' => 'Y-m-d H:i:s',
        'updated_at' => 'Y-m-d H:i:s',
    ];

    static function getBehaviorName($behavior)
    {
        $behavior_name =  [
            '0' => '订单创建',
            '1' => '订单支付',
            '2' => '订单发货',
            '3' => '订单作废',
            '4' => '订单完成',
        ];

        return $behavior_name[$behavior];
    }

    public function getMemberNameAttribute()
    {
        $member = Member::where('member_id', $this->attributes['member_id'])->first();
        if ($member)
        {
            return $member->name;
        }
    }

    /**
     * 订单信息
     * @return \Illuminate\Database\Eloquent\Relations\belongsTo
     */
    public function order()
    {
        return $this->belongsTo(order::class, 'order_id', 'order_id');
    }

    public static function getDeliveryLog(array $attributes)
    {
        extract($attributes);

        $member =  \Auth::guard('member')->user();

        $memberBind = MemberBindAdmin::where('member_id', $member->member_id)->first();

        $where = ['source'=>'admin', 'op_id'=>$memberBind->admin_id, 'behavior'=>self::ORDER_DELIVERY];

        $model = OrderLog::where($where);

        $total = $model->count();

        $model->orderBy('updated_at', 'DESC');

        if (isset($per_page))
        {
            $data = $model->paginate($per_page)->toArray();

            if (!empty($data['data']))
            {
                foreach ($data['data'] as $key=>$val)
                {
                    if (!$val) continue;

                    $data['data'][$key]['created_at'] = date('Y-m-d H:i:s', strtotime($val['created_at']));
                    $data['data'][$key]['updated_at'] = date('Y-m-d H:i:s', strtotime($val['updated_at']));
                }
            }

            return self::formatBody(['list' => $data['data'], 'paged' => self::formatPaged($page, $per_page, $total)]);
        }
        else
        {
            $data = $model->get()->toArray();

            if (!empty($data))
            {
                foreach ($data as $key=>$val)
                {
                    if (!$val) continue;

                    $data[$key]['created_at'] = date('Y-m-d H:i:s', strtotime($val['created_at']));
                    $data[$key]['updated_at'] = date('Y-m-d H:i:s', strtotime($val['updated_at']));
                }
            }

            return self::formatBody(['list' => $data]);
        }
    }

}
